package nl.hu.modellingengine.ribbon.client.view;

import java.util.ArrayList;

import nl.hu.modellingengine.ribbon.client.places.ElementPositionChangePlace;

import com.allen_sauer.gwt.dnd.client.DragEndEvent;
import com.allen_sauer.gwt.dnd.client.DragHandler;
import com.allen_sauer.gwt.dnd.client.DragStartEvent;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.VetoDragException;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class RibbonViewWebImpl extends Composite implements RibbonView {

	private static RibbonViewWebImplUiBinder uiBinder = GWT
			.create(RibbonViewWebImplUiBinder.class);
	
	private Presenter presenter;
	
	private PickupDragController pickupDragController = new PickupDragController(RootPanel.get(),true);
	
	@UiField TabLayoutPanel tabRibbon;

	interface RibbonViewWebImplUiBinder extends
			UiBinder<Widget, RibbonViewWebImpl> {
	}

	public RibbonViewWebImpl() {
		pickupDragController.addDragHandler(new DragListener());
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setRibbons(ArrayList<RibbonColumn> ribbons) {
		if(tabRibbon.getWidgetCount()>0){
			tabRibbon.clear();
		
		}
		
		for(RibbonColumn ribbon : ribbons){	
			Boolean tabExist= false;
			for(int i=0;i<tabRibbon.getWidgetCount();i++){
				Label lblTab = (Label)tabRibbon.getTabWidget(i);
				if(ribbon.getStrTab().equals(lblTab.getText())){
					HorizontalPanel pnlTab = (HorizontalPanel) tabRibbon.getWidget(i);
					Image img = new Image(GWT.getHostPageBaseURL() + ribbon.getStrImgSource());
					img.getElement().setId(Document.get().createUniqueId());
					img.setAltText(ribbon.getStrName());
					pickupDragController.makeDraggable(img);
					pnlTab.add(img);
				tabExist = true;
				}
			}
			if(!tabExist){
				HorizontalPanel pnlTab = new HorizontalPanel();
				Image img = new Image(GWT.getHostPageBaseURL()+ ribbon.getStrImgSource());
				img.setAltText(ribbon.getStrName());
				img.getElement().setId(Document.get().createUniqueId());
				pickupDragController.makeDraggable(img);
				pnlTab.add(img);
				tabRibbon.add(pnlTab,ribbon.getStrTab());
			}			
		}
	}

	@Override
	public void addRibbon(RibbonColumn ribbon) {
		// TODO Auto-generated method stub	
	}

	@Override
	public void removeRibbon(RibbonColumn ribbon) {
		// TODO Auto-generated method stub	
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	private class DragListener implements DragHandler{

		@Override
		public void onDragEnd(DragEndEvent event) {
			String token = "type=" + ((Image)event.getContext().draggable).getAltText();
			token = token + "&id=" + event.getContext().draggable.getElement().getId();	
			presenter.goTo(new ElementPositionChangePlace(token));
		}

		@Override
		public void onDragStart(DragStartEvent event) {
		}

		@Override
		public void onPreviewDragEnd(DragEndEvent event)
				throws VetoDragException {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPreviewDragStart(DragStartEvent event)
				throws VetoDragException {
			// TODO Auto-generated method stub
			
		}
		
	}

}
